﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using Dict.Config;

namespace UI
{

    public class UIFieldRunMapCombatLogTemplate : MonoBehaviour
    {
        public bool leftWin = false;

        public GameObject bkObj;
        public GameObject vs0, vs1;
        public UITexture leftHeroIcon, rightHeroIcon;
        public UISprite leftHeroFrame, rightHeroFrame;
        public UILabel leftHeroName, rightHeroName;
        SidePvpMV sideMv;
        public UILabel battleLog;
        public GameObject fightBtn;

        public void Init(SidePvpMV side)
        {
            this.sideMv = side;
            bool isAtt=false, isWin = false;
            if (StaticData.playerData.name == side.attPlayerName)//攻击者
            {
                isAtt = true;
                isWin = side.attPlayerName == side.winPlayerName;
                SetWinner(isWin);
                SetHero(side.attHeroId, side.defHeroId);
                SetPlayerName(side.attPlayerName, side.defPlayerName);
                if (!isWin)
                {
                    leftHeroIcon.material = AssetLoad.ResourceManager.Instance.LoadResource("shaders/heroIcon_circle_colorness") as Material;
                    leftHeroIcon.material.shader = Shader.Find("Unlit/Masked DJMaskColorless");
                    leftHeroFrame.spriteName = "UI_kuang_big_yuandi_white";
                }
                else
                {
                    rightHeroIcon.material = AssetLoad.ResourceManager.Instance.LoadResource("shaders/heroIcon_circle_colorness") as Material;
                    rightHeroIcon.material.shader = Shader.Find("Unlit/Masked DJMaskColorless");
                    rightHeroFrame.spriteName = "UI_kuang_big_yuandi_white";
                }
            }
            else//防御者
            {
                isWin = StaticData.playerData.name == side.winPlayerName;

                SetWinner(side.defPlayerName == side.winPlayerName);
                SetHero(side.defHeroId, side.attHeroId);
                SetPlayerName(side.defPlayerName, side.attPlayerName);
                if (!isWin)
                {
                    leftHeroIcon.material = AssetLoad.ResourceManager.Instance.LoadResource("shaders/heroIcon_circle_colorness") as Material;
                    leftHeroIcon.material.shader = Shader.Find("Unlit/Masked DJMaskColorless");
                    leftHeroFrame.spriteName = "UI_kuang_big_yuandi_white";
                }
                else
                {
                    rightHeroIcon.material = AssetLoad.ResourceManager.Instance.LoadResource("shaders/heroIcon_circle_colorness") as Material;
                    rightHeroIcon.material.shader = Shader.Find("Unlit/Masked DJMaskColorless");
                    rightHeroFrame.spriteName = "UI_kuang_big_yuandi_white";
                }

            }
            battleLog.text = sideMv.time+"";
            
            var hour = ServerTimerTool.Java2CSTimeLocale(sideMv.time).Hour;
            var minute = ServerTimerTool.Java2CSTimeLocale(sideMv.time).Minute;

            int num = 0;
            foreach (var it in sideMv.resource)
            {
                if (it.id == TableConst.coin)
                {
                    num += it.num;
                }
            }
            

            string strTime = hour + ":" + minute;

            if (isWin && isAtt)
            {
                battleLog.text = string.Format( TableTextBlo.GetText("field_att_succecc"), strTime,num);
            }
            else if(isWin && !isAtt)
            {
                battleLog.text = string.Format(TableTextBlo.GetText("field_def_succecc"), strTime);
            }
            else if (!isWin && isAtt)
            {
                battleLog.text = string.Format(TableTextBlo.GetText("field_att_lost"), strTime);
            }
            else
            {
                battleLog.text = string.Format(TableTextBlo.GetText("field_def_lost"), strTime, num);
            }

            if (CommonMethod.CompareVersion(side.appVersion, side.resVersion, GameConfig.Instance.AppVersion, GameConfig.Instance.ResVersion))
            {
                fightBtn.SetActive(true);
            }
            else
            {
                fightBtn.SetActive(false);
            }
        }

        void SetWinner(bool leftWin)
        {
            this.leftWin = leftWin;
            this.bkObj.transform.localScale = new Vector3(this.leftWin? -1:1, 1, 1);
            this.vs1.SetActive(!leftWin);
            this.vs0.SetActive(leftWin);
        }

        void SetHero(string leftHeroId, string rightHeroId)
        {
            var leftHero = Dict.Blo.DictHeroBlo.GetTableHero(leftHeroId);
            var rightHero = Dict.Blo.DictHeroBlo.GetTableHero(rightHeroId);

            leftHeroIcon.GetComponent<TextureKit>().iconType = TextureIcon.HERO;
            leftHeroIcon.GetComponent<TextureKit>().textureName = leftHero.iconName;
            rightHeroIcon.GetComponent<TextureKit>().iconType = TextureIcon.HERO;
            rightHeroIcon.GetComponent<TextureKit>().textureName = rightHero.iconName;

            leftHeroFrame.spriteName = CommonMethod.GetQualityBack((QualityValue)leftHero.quality, true);
            rightHeroFrame.spriteName = CommonMethod.GetQualityBack((QualityValue)rightHero.quality, true);
            
        }
        

        void SetPlayerName(string leftHeroName,string rightHeroName)
        {
            this.leftHeroName.text = leftHeroName;
            this.rightHeroName.text = rightHeroName;
        }

        public void OpenMoive()
        {
            CSSideGetPvpMV mv = new CSSideGetPvpMV();
            mv.fightId = sideMv.fightId;
            StaticData.sidePvpMv = sideMv;
            NetAPI.SendHttp(OpDefine.CSSideGetPvpMV, mv, (obj, ok) => 
            {
                if(ok)
                {
                    
                    SCSideGetPvpMV msg = (SCSideGetPvpMV)obj.kBody;
                    StaticData.sideGetPvpMv = msg;
                    StaticData.curBattleData.SetData(msg.data);
                    ManagerController.Instance.FieldToBattle();
                }
            });
        }
    }
}